package com.rock.upms.web.permission.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.rock.common.base.BaseController;
import com.rock.common.base.BaseQuery;
import com.rock.common.base.BaseResult;
import com.rock.common.util.StringUtil;
import com.rock.upms.api.model.service.IUpmsPermissionService;
import com.rock.upms.model.UpmsPermission;
import com.rock.upms.model.UpmsPermissionExample;
import com.rock.upms.model.UpmsPermissionExample.Criteria;
import com.rock.upms.web.common.config.WebConfig;

@Controller
@RequestMapping(value = "upms/permission")
public class UpmsPermissionController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(UpmsPermissionController.class);
	@Autowired
	private IUpmsPermissionService upmsPermissionService;

	/**
	 * 功能： 列表初始化<br>
	 * 说明：点击菜单或链接进行列表初始化，初始化默认查询条件等<br>
	 * 请求方式：链接（GET）
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions(value = "upms:system:list")
	public String toList(Model model) {
		BaseQuery query = new BaseQuery(1, WebConfig.getPageSize(), true);// 构造函数按默认分页方式分页，从第一页开始
		// query.add("isDelete", "0");
		model.addAttribute("query", query);
		return "upms/permission/upmsPermission";
	}

	/**
	 * 功能：列表查询<br>
	 * 说明：<br>
	 * （1）列表初始化完成后显示初始化数据<br>
	 * （2）页面查询条件发生改变后的查询<br>
	 * 请求方式：ajax post请求
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions(value = "upms:system:list")
	public BaseResult list(BaseQuery query) {
		int pageNum = query.getPageNum();// 页号
		int dataNum = query.getPageSize();// 每页显示记录数
		int navigatePages = WebConfig.getPageNavigatePages();// 导航显示页数
		// if (!query.getIsPagination()) {
		// pageSize = 0;// 不分页
		// }
		// 封装查询条件（自动生成三个，请根据实际修改）
		// String isDelete = (String) query.getExtend().get("isDeleted");// 是否删除
		// String usercode = (String) query.getExtend().get("usercode");// 机构号
		// String username = (String) query.getExtend().get("username");//
		// 机构名模糊匹配
		String orderBy = " id desc ";// 默认ID降序
		UpmsPermissionExample upmsPermissionExample = new UpmsPermissionExample();
		Criteria criteria = upmsPermissionExample.or();
		// if (StringUtil.isNotBlank(isDelete)) {
		// criteria.andIsDeletedEqualTo(isDelete);
		// }
		// if (StringUtil.isNotBlank(usercode)) {
		// criteria.andUsercodeEqualTo(usercode);
		// }
		// if (StringUtil.isNotBlank(username)) {
		// criteria.andUsernameLike("%" + username + "%");
		// }
		upmsPermissionExample.setOrderByClause(orderBy);
		List<UpmsPermission> deptList = upmsPermissionService.selectByExample(upmsPermissionExample);
		PageInfo<UpmsPermission> page = new PageInfo<UpmsPermission>(deptList, navigatePages);
		if (page != null) {
			return BaseResult.success("").add("pageInfo", page);
		} else {
			return BaseResult.fail("获取数据失败");
		}
	}

	/**
	 * 功能：新增（初始化）<br>
	 * 请求方式：ajax get
	 * 
	 * @return Msg的json串
	 */
	@ResponseBody
	// @RequiresPermissions("upms:system:add")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public BaseResult toAdd() {
		UpmsPermission vo = new UpmsPermission();
		// vo.setCreateUsercode(UserUtil.getUsercode());
		// vo.setCreateUsername(UserUtil.getUsername());
		// vo.setCreateTime(new Date());
		return BaseResult.success("").add("upmsPermission", vo);
	}

	/**
	 * 功能：新增（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:add")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public BaseResult add(UpmsPermission vo) {
		log.trace("--->add vo:" + vo.toString());
		// vo.setCreateUsercode(UserUtil.getUsercode());
		// vo.setCreateUsername(UserUtil.getUsername());
		// vo.setCreateTime(new Date());
		// vo.setIsDeleted("0");
		int result = upmsPermissionService.insert(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：删除 <br>
	 * 说明：根据主键ID删除记录，ID未加密
	 * 
	 * @param cryptId
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public BaseResult delete(@PathVariable("id") String id) {
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			int backint = upmsPermissionService.deleteByPrimaryKey(idint);
			if (backint != 0) {
				return BaseResult.success("删除成功");
			} else {
				return BaseResult.fail("删除失败");
			}
		}
	}

	/**
	 * 功能：修改（初始化）<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public BaseResult toEdit(@PathVariable("id") String id) {
		UpmsPermission vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsPermission) upmsPermissionService.selectByPrimaryKey(idint);
		}
		return BaseResult.success("").add("upmsPermission", vo);
	}

	/**
	 * 功能：修改（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public BaseResult edit(UpmsPermission vo) {
		log.trace("--->edit vo:" + vo.toString());
		int result = upmsPermissionService.updateByPrimaryKey(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：查看<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:read")
	@RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
	public BaseResult read(@PathVariable("id") String id) {
		UpmsPermission vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsPermission) upmsPermissionService.selectByPrimaryKey(idint);
		}
		return BaseResult.success("").add("upmsPermission", vo);
	}
}